﻿/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.views.platformer.movieclips {
	import com.puny.mvc.models.EntityModels;
	import com.puny.mvc.models.Model;
	import com.puny.xmlutils.XMLReader;
	
	import flash.events.Event;
	import flash.geom.Point;
	
	public class MainCharacterView extends VerletCharacterView {
		
		public static const MOVING:String = "main character moving";
		private var _screenX:Number;
		private var _screenY:Number;
		private var _model:Model;
		private var _cx:Number = 0;
		private var _cy:Number = 0;
		private var _movingEvent:Event = new Event(MOVING);

		public function MainCharacterView() {
			super();
			
			try {
			_model = EntityModels.getInstance(PlatformerConfigure.MAIN_CHARACTER);
			var xml:XML = XMLReader.xmlDoc(PlatformerConfigure.CONFIGURE).mainCharacter[0];
			_model.screenX = int(xml.@screenX);
			_model.screenY = int(xml.@screenY);
			_model.maxJump = int(xml.@maxJump);
			_model.jump = int(xml.@jump);
			_model.left = new Point(-int(xml.@speed),0);
			_model.right = new Point(int(xml.@speed),0);
			_screenX = _model.screenX;
			_screenY = _model.screenY;
			} catch(e:Error) {
				
			}
		}
		
		protected function get model():Model {
			return _model;
		}
		
		override public function beforeReady():void {
			super.beforeReady();
			gotoAndStop(0);
		}
		
		override public function gotoAndPlay(frame:Object,scene:String=null):void {
			super.gotoAndPlay(frame,scene);
		}
		
		override public function gotoAndStop(frame:Object,scene:String=null):void {
			super.gotoAndStop(frame,scene);
		}
		
		override protected function animation():Boolean {
			var ret:Boolean = super.animation();
			if (currentLabel.indexOf("dance") >= 0) {
				dispatchEvent(_movingEvent.clone());	
			}
			return ret;
		}
		
		override public function set x(value:Number):void {
			if (x == value) {
				return;
			} else if (x < value) {
				if (_cx > -50) {
					_cx--;
				}
			} else {
				if (_cx < 50) {
					_cx++;
				}
			}
			
			_screenX = _model.screenX+_cx;// + (root as TrixSwirls).frameW;

			super.x = value;
			dispatchEvent(_movingEvent.clone());
		}
		
		override public function set y(value:Number):void {
			if (int(y) == int(value)) {
				return;
			} else if (int(y) < int(value)) {
				if (_cy > -50) {
					_cy--;
				}
			} else {
				if (_cy < 50) {
					_cy++;
				}
				
			}

			_screenY = PlatformerConfigure.HEIGHT - _model.screenY -_cy;

			super.y = value;
			dispatchEvent(_movingEvent.clone());
		}
		
		public function get screenX():Number {
			return _screenX;
		}
		
		public function get screenY():Number {
			return _screenY;
		}
		
		override public function afterClose():void {
			super.afterClose();
		}
		
	}
}